<template lang="">
    <div>
        <el-form ref="form" :model="form" label-width="80px">
            <el-row :gutter="20">
                <el-col :span="2">
                    <el-button type="text" icon="el-icon-delete" @click="deleteSelected()">删除选中</el-button>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="学生学号:">
                        <el-input v-model="form.stuNum"></el-input>
                    </el-form-item>
                </el-col>

                <el-col :span="8">
                    <el-form-item label="学生姓名:">
                        <el-input v-model="form.stuName"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-button type="primary" round icon="el-icon-search" @click="toSearch">开始查找</el-button>
                    <el-button type="danger" round icon="el-icon-close" @click="clear">清空</el-button>
                </el-col>
            </el-row>
        </el-form>
    </div>
</template>
<script>
    export default {
        name: 'Search',
        data() {
            return {
                form: {
                    stuNum: '',
                    stuName: ''
                },
                selectedId: []
            }
        },
        methods: {
            toSearch() {
                console.log(this.form);
                let isNum = /\d/
                console.log('学号是数字吗?',isNum.test(this.form.stuNum));
                console.log('姓名是数字吗?',isNum.test(this.form.stuName));

                if (this.form.stuNum == "") {
                    if(!isNum.test(this.form.stuName)){
                        axios({
                            method: 'GET',
                            url: encodeURI(`/student/search/${this.form.stuName}`),
                            withCredentials: true,
                            headers: {
                                'Content-Type': 'application/json;charset=utf-8'
                            }
                        }).then((result) => {
                            let data = result.data.data
                            this.$bus.$emit('search', data)
                            if (Array.isArray(data)) {
                                this.$bus.$emit('totalPage', data.length)
                            } else {
                                this.$bus.$emit('totalPage', 1)
                            }
                        }).catch((err) => {
                            this.$message({
                                message: err.message,
                                type: 'info'
                            })
                        });
                    }else{
                        this.$message({
                            message:'请输入正确的姓名',
                            type:'warning'
                        })
                    }
                } else {
                    if(isNum.test(this.form.stuNum)){
                        axios({
                            method: 'GET',
                            url: `/student/list/${this.form.stuNum}`,
                            withCredentials: true,
                            headers: {
                                'Content-Type': 'application/json'
                            }
                        }).then((result) => {
                            let data = result.data.data
                            this.$bus.$emit('search', data)
                            this.$bus.$emit('totalPage',1)
                        }).catch((err) => {
                            this.$message({
                                message: err.message,
                                type: 'info'
                            })
                        });
                    }else{
                        this.$message({
                            message:'请输入正确的学号',
                            type:'warning'
                        })
                    }
                }
            },
            clear() {
                this.form = {
                    stuNum: '',
                    stuName: ''
                }
                this.$bus.$emit('clearSearch')
            },
            deleteSelected() {
                axios({
                    method: 'DELETE',
                    url: '/student/ids',
                    data: this.selectedId,
                    withCredentials: true,
                    headers: {
                        'Content-Type': 'application/json'
                    }
                }).then((result) => {
                    this.$bus.$emit('sthSuccess')
                }).catch((err) => {
                    this.$message({
                        message: err.message,
                        type: 'info'
                    })
                });
            }
        },
        mounted() {
            this.$bus.$on('select', (selectedId) => {
                this.selectedId = selectedId
            })
            this.$bus.$on('toSearch',()=>{
                this.toSearch()
            })
        },
        beforeDestroy() {
            this.$bus.$off('select')
            this.$bus.$off('toSearch')
        },
    }
</script>
<style lang="">

</style>